package com.tc.zigbee.mt.request;

import com.tc.util.DataUtil;
import com.tc.zigbee.entity.Endpoint;
import com.tc.zigbee.mt.MtCommand;
/*
TVSA_DONGLE=1
TVSA_SRC_RTG=1
CONCENTRATOR_ENABLE=1
CONCENTRATOR_DISCOVERY_TIME=60
SRC_RTG_EXPIRY_TIME=255
MAX_RTG_SRC_ENTRIES=100
*/
public class RequestFactory{


//	Byte: 1  1  1  2  1  1 
//	Length = 0x04  Cmd0 = 0x25  Cmd1 = 0x01  ShortAddr  ReqType  StartIndex 
	
	static public MtCommand MtIEEEAddrRequest(short sa, byte si, byte rt){
		System.out.println(DataUtil.hex(sa));
		MtCommand mc = new MtCommand();
		mc.length = 0x04;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x01;
		
		byte[] shortAddr;
		byte reqType;
		byte startIndex;
		shortAddr = DataUtil.bytesValue(sa);
		reqType = rt;
		startIndex = si;
		mc.command = new byte[mc.length];
		mc.command[0] = shortAddr[0];
		mc.command[1] = shortAddr[1];
		mc.command[2] = reqType;
		mc.command[3] = startIndex;
		mc.fillFrame();
		return mc;
	}
	

	static public MtCommand MtAfDataRequest(byte addrMode, short da, byte destEndpoint,
	byte srcEndpoint,short cid, byte transId, byte options,	
	byte radius,byte len, byte[] data){
		MtCommand mc = new MtCommand();

		mc.cmd0 = 0x24;
		mc.cmd1 = 0x01;
		
		
		byte[] dstAddr = DataUtil.bytesValue(da);
		

		byte[] clusterId  = DataUtil.bytesValue(cid);


			len = (byte)data.length;
			mc.length = (byte)(11 + len);
			mc.command = new byte[mc.length];
			mc.command[0] = addrMode;
			mc.command[1] = dstAddr[0];
			mc.command[2] = dstAddr[1];
			mc.command[3] = destEndpoint;
			mc.command[4] = srcEndpoint;
			mc.command[5] = clusterId[0];
			mc.command[6] = clusterId[1];
			mc.command[7] = transId;
			mc.command[8] = options;
			mc.command[9] = radius;
			mc.command[10] = len;
			for(int i = 0; i < data.length; i++)
				mc.command[11 + i] = data[i];
			mc.fillFrame();
			return mc;
		}
	{
	//	Length = 0x04  Cmd0 = 0x25  Cmd1 = 0x05  DstAddr  NWKAddrOfInterest 
	}
	static public MtCommand MtActiveEndpointRequest(short sa, short noi){
		MtCommand mc = new MtCommand();
		mc.length = 0x04;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x05;
		
		byte[] shortAddr = DataUtil.bytesValue(sa);
		byte[] nwk = DataUtil.bytesValue(noi);
		mc.command = new byte[mc.length];
		mc.command[0] = shortAddr[0];
		mc.command[1] = shortAddr[1];
		mc.command[2] = nwk[0];
		mc.command[3] = nwk[1];
		mc.fillFrame();
		return mc;
	}
	//Length = 0x05  Cmd0 = 0x25  Cmd1 = 0x04  DstAddr  NWKAddrOfInterest  Endpoint 
	static public MtCommand MtSimpleDescRequest(short da, short noi,byte ep){
		MtCommand mc = new MtCommand();
		mc.length = 0x05;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x04;
		
		byte[] destAddr = DataUtil.bytesValue(da);
		byte[] noia = DataUtil.bytesValue(noi);
		mc.command = new byte[mc.length];
		mc.command[0] = destAddr[0];
		mc.command[1] = destAddr[1];
		mc.command[2] = noia[0];
		mc.command[3] = noia[1];
		mc.command[4] = ep;
		mc.fillFrame();
		return mc;
	}
	static public MtCommand MtNodeDescRequest(short da, short noi){
		MtCommand mc = new MtCommand();
		mc.length = 0x04;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x02;
		
		byte[] destAddr = DataUtil.bytesValue(da);
		byte[] noia = DataUtil.bytesValue(noi);
		mc.command = new byte[mc.length];
		mc.command[0] = destAddr[0];
		mc.command[1] = destAddr[1];
		mc.command[2] = noia[0];
		mc.command[3] = noia[1];
		mc.fillFrame();
		return mc;
	}
	static public MtCommand MtPowerDescRequest(short da, short noi){
		MtCommand mc = new MtCommand();
		mc.length = 0x04;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x03;
		
		byte[] destAddr = DataUtil.bytesValue(da);
		byte[] noia = DataUtil.bytesValue(noi);
		mc.command = new byte[mc.length];
		mc.command[0] = destAddr[0];
		mc.command[1] = destAddr[1];
		mc.command[2] = noia[0];
		mc.command[3] = noia[1];
		mc.fillFrame();
		return mc;
	}
	static public MtCommand MtComplexDescRequest(short da, short noi){
		MtCommand mc = new MtCommand();
		mc.length = 0x04;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x07;
		
		byte[] destAddr = DataUtil.bytesValue(da);
		byte[] noia = DataUtil.bytesValue(noi);
		mc.command = new byte[mc.length];
		mc.command[0] = destAddr[0];
		mc.command[1] = destAddr[1];
		mc.command[2] = noia[0];
		mc.command[3] = noia[1];
		mc.fillFrame();
		return mc;
	}

//		1						1			1			2			8			1			2
//		Length = 0x10-0x17	Cmd0 = 0x25	Cmd1 = 0x21	DstAddr		SrcAddress	SrcEndpoint	ClusterId
//		1						2/8			0/1
//		DstAddrMode			DstAddress		DstEndpoint

	
	
//	ADDRESS_NOT_PRESENT	0x00	Address Not Present
//	GROUP_ADDRESS	0x01	Group address
//	ADDRESS_16_BIT	0x02	Address 16 bit
//	ADDRESS_64_BIT	0x03	Address 64 bit
//	BROADCAST	0xFF	Broadcast
	static public MtCommand MtBindRequest(short da, long srcAddress, byte srcEndpoint, short clusterId, long dstAddress, byte dstEndpoint){
		MtCommand mc = new MtCommand();
		mc.length = 23;
		mc.cmd0 = 0x25;
		mc.cmd1 = 0x21;
		mc.command = new byte[mc.length];
		int i = 0;
		 i += DataUtil.bytesValue(da, mc.command,  i);
		i += DataUtil.bytesValue(srcAddress, mc.command,  i);
		
		
		mc.command[i++] = srcEndpoint;
		 i += DataUtil.bytesValue(clusterId, mc.command,  i);
		mc.command[i++] = 0x03;
		 i += DataUtil.bytesValue(dstAddress, mc.command,  i);
		mc.command[i] = dstEndpoint;

		mc.fillFrame();
		return mc;
	}
//	
//	Length = 0x09-0x49  Cmd0 = 0x24  Cmd1 = 0x00  EndPoint  AppProfId  AppDeviceId 
//	 
//	1  1  1  0-32  1  0-32 
//	AppDevVer  LatencyReq  AppNumInClusters  AppInClusterList  AppNumOutClusters  AppOutClusterList 
	static public MtCommand MtAfRegister(Endpoint ep){
		MtCommand mc = new MtCommand();
		//mc.length = 0x05;
		mc.cmd0 = 0x24;
		mc.cmd1 = 0x00;
		
		mc.length = (byte)(9 + ep.getInClusters().size() * 2 + ep.getOutClusters().size() * 2);
		mc.command = new byte[mc.length];
		
		mc.command[0] = ep.getEndpointId();
		byte[] b = DataUtil.bytesValue(ep.getProfileId());
		mc.command[1] = b[0];
		
		mc.command[2] = b[1];
		b = DataUtil.bytesValue(ep.getDeviceId());
		mc.command[3] = b[0];
		mc.command[4] = b[1];
		
		mc.command[5] = ep.getDeviceVersion();
		mc.command[6] = ep.getLatency();
		mc.command[7] = (byte)ep.getInClusters().size();
		for(int i = 0; i < ep.getInClusters().size(); i++){
			b = DataUtil.bytesValue(ep.getInClusters().get(i));
			mc.command[8 + 2 * i] = b[0];
			mc.command[8 + 2 * i + 1] = b[1];
			
		}
		mc.command[8 + 2 * ep.getInClusters().size()] = (byte)ep.getOutClusters().size();
		
		for(int i = 0; i < ep.getOutClusters().size(); i++){
			b = DataUtil.bytesValue(ep.getOutClusters().get(i));
			mc.command[9 + 2 * ep.getInClusters().size() + 2 * i] = b[0];
			mc.command[9 + 2 * ep.getInClusters().size() + 2 * i + 1] = b[1];
			
		}
		mc.fillFrame();
		return mc;
	}

}
